import { computed, onUnmounted } from 'vue'
import { useStore } from 'vuex'

export function initErcode(tabName: any, dialogVisible: any) {
  const store = useStore()
  const ercodeImg = computed(() => store.state.login.ercodeImg)
  // 二维码登录
  const handleTabClick = () => {
    if (tabName.value === 'ercode') {
      store.dispatch('login/erCodeLoginAction')
    } else {
      store.commit('login/clearTimer')
    }
  }
  const isErState = computed(() => {
    if (store.state.login.erCodeState === true) {
      dialogVisible.value = false
      tabName.value = 'account'
    }
    return store.state.login.erCodeState
  })
  // 弹窗关闭
  const handleCloseDialog = () => {
    store.commit('login/clearTimer')
    tabName.value = 'account'
  }
  // 页面卸载
  onUnmounted(() => {
    store.commit('login/clearTimer')
    tabName.value = 'account'
  })
  return {
    handleTabClick,
    isErState,
    handleCloseDialog,
    ercodeImg
  }
}
